package it.luigiarpino.sudoku;



import javax.swing.table.AbstractTableModel;

    class MyTableModelSudoku extends AbstractTableModel {
        
        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		String[] columnNames; 
        Object[][] dati;
        Sudoku s;
        
    public MyTableModelSudoku(Sudoku s) {
        this.s=s;
        dati = new Object [9][9]; 
	for (int i=1;i<=9;i++){
            for (int j=1;j<=9;j++){
                if (s.GetElemSchema(i,j).intValue()!=0){
                    this.dati[i-1][j-1]=String.valueOf(s.GetElemSchema(i,j).intValue());
                }
                else {
                    this.dati[i-1][j-1]="";
                }
                
            }
        }
        this.columnNames=new String[9];
     }//fine costruttore        
        
    
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return dati.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            /*
            Integer numero;
            try{
                numero=new Integer(dati[row][col].toString());
                if (numero.intValue()<1 || numero.intValue() >9){
                    return dati[row][col];
                }
                else{
                    return " ";
                }
            }
            catch (java.lang.Exception e){
                return " ";
            }
             */
            return dati[row][col];
        }
            

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the last column would contain text ("true"/"false"),
         * rather than a check box.
         */
        
        public Class getColumnClass(int c)
        {
            return getValueAt(0, c).getClass();
        }
        

        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            /*
             if (s.GetElemSchema(row+1,col+1).intValue()==0){
                return true;
            }
            else {
                return false;
            }
             */
            return true;
        }

        public void setValueAt(Object value, int row, int col) {
            
            Integer numero;
            try{
                numero=new Integer(value.toString());
                if (numero.intValue()<1 || numero.intValue() >9){
                    dati[row][col]="";
                }
                else{
                    dati[row][col]=value.toString();
                }
            }
            catch (java.lang.Exception e){
                numero= new Integer(0);
                dati[row][col]="";
            }
            for (int i=1;i<=9;i++){
                for (int j=1;j<=9;j++){
                    if (s.GetElemSchema(i,j).intValue()!=0){
                        this.dati[i-1][j-1]=String.valueOf(s.GetElemSchema(i,j).intValue());
                    }
                    fireTableCellUpdated(i-1, j-1);
                }
            }
            fireTableCellUpdated(row, col);// Ridondante
        }
        


    }// Fine classe
